package com.smfx.yxt.controller.sys.kccs;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.smfx.yxt.common.RspResult;
import com.smfx.yxt.common.enums.Status;
import com.smfx.yxt.controller.BaseController;
import com.smfx.yxt.model.Grid;
import com.smfx.yxt.model.KccsCourseInfo;
import com.smfx.yxt.page.OrderBy;
import com.smfx.yxt.page.Page;
import com.smfx.yxt.reqVo.KccsCourseQueryVO;
import com.smfx.yxt.service.IKccsCourseInfoService;
import com.smfx.yxt.service.IStudentService;
import com.smfx.yxt.utils.ExcelExportUtil;
import com.smfx.yxt.utils.ExportSetInfoUtil;
import com.smfx.yxt.view.KccsCourseStudentSignTotal;
import com.smfx.yxt.view.TermView;

@RequestMapping("sys/course/courseClazzTotal")
@Controller
public class SysCourseClazzTotalController extends BaseController {

	@Autowired
	private IKccsCourseInfoService kccsCourseInfoService;
	@Autowired
	private IStudentService studentService;
	
	@RequestMapping(value={"","/"})
	public String Index() {
		return "sys/kccs/courseClazzTotal";
	}
	
	@RequestMapping("getHeader")
	@ResponseBody Object getHeader(KccsCourseQueryVO vo) {
		List<String> headers = new ArrayList<>();
		String[] array = vo.getGradeType().split(",");
		vo.setGrade(Integer.valueOf(array[0]));
		vo.setStudyType(Integer.valueOf(array[1]));
		List<Integer> clazzList = studentService.getFullClazz(
				Integer.valueOf(array[0]), Integer.valueOf(array[1]));
		headers.add("课程分类");
		headers.add("课程编号");
		headers.add("课程名称");
		headers.add("上课时间");
		for(Integer v : clazzList) {
			headers.add(TermView.getGradeName(vo.getGrade(),vo.getStudyType())+v+"班");
		}
		headers.add("总报名人数");
		headers.add("剩余名额");
		return new RspResult(Status.SUCCESS, String.join(",", headers));
	}
	
	@RequestMapping("pageQuery")
	@ResponseBody Object pageQuery(KccsCourseQueryVO vo){
		try {
			if(vo.getTermId() == null) {
				vo.setTermId(getCurrentTerm().getId());
			}
			String[] array = vo.getGradeType().split(",");
			vo.setGrade(Integer.valueOf(array[0]));
			vo.setStudyType(Integer.valueOf(array[1]));
			List<Map<String, String>> resultList = new ArrayList<>();
			List<Integer> clazzList = studentService.getFullClazz(Integer.valueOf(array[0]), Integer.valueOf(array[1]));
			List<KccsCourseStudentSignTotal> totalList = kccsCourseInfoService.selectCourseClazzTotal(vo);
			Map<String, Integer> totalMap = new HashMap<>();
			Map<String, Integer> gradeMap = new HashMap<>();
			for(KccsCourseStudentSignTotal total : totalList) {
				totalMap.put(total.getCourseId()+total.getSignClassDate()+total.getGrade()+total.getClazz(), total.getNum());
				String gradeKey = total.getCourseId()+total.getSignClassDate();
				if(!gradeMap.containsKey(total.getCourseId()+total.getSignClassDate())) {
					gradeMap.put(gradeKey, total.getNum());
					continue;
				}
				gradeMap.put(gradeKey, gradeMap.get(gradeKey)+total.getNum());
			}
			vo.setIsAgree(1);
			Page<KccsCourseInfo> page = new Page<>();
			page.setPageSize(Integer.MAX_VALUE - 1);
			page.setParams(vo);
			List<OrderBy> orderBys = new ArrayList<>();
			orderBys.add(new OrderBy("COURSE_NO"));
			page.setSorts(orderBys);
			List<KccsCourseInfo> list = kccsCourseInfoService.selectListByPage(page);
			for(KccsCourseInfo kc : list) {
				if(StringUtils.isNotBlank(kc.getClassDate())) {
					String[] cs = kc.getClassDate().split(",");
					for(String c : cs) {
						Map<String, String> map = new LinkedHashMap<>();
						map.put("课程分类", kc.getCourseType());
						map.put("课程编号", kc.getCourseNo());
						map.put("课程名称", kc.getCourseName());
						map.put("上课时间", c);
						for(Integer clazz : clazzList) {
							Integer num = totalMap.get(kc.getId()+c+vo.getGrade()+clazz);
							map.put(TermView.getGradeName(vo.getGrade(),vo.getStudyType())+clazz+"班", String.valueOf(num != null ? num : 0));
						}
						Integer totalNum = gradeMap.get(kc.getId()+c);
						totalNum = totalNum == null ? 0 : totalNum;
						map.put("总报名人数", String.valueOf(totalNum));
						map.put("剩余名额", String.valueOf(kc.getLimitNum().intValue() - totalNum.intValue()));
						resultList.add(map);
					}
				}
			}
			Grid grid = new Grid();
			grid.setRows(resultList);
			grid.setTotal(resultList.size());
			return resultList;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	@RequestMapping("export")
	ModelAndView workSignExport(KccsCourseQueryVO vo) {
		ModelAndView modelAndView = new ModelAndView();
		try {
			if(vo.getTermId() == null) {
				vo.setTermId(getCurrentTerm().getId());
			}
			List<String> headers = new ArrayList<>();
			String[] array = vo.getGradeType().split(",");
			vo.setGrade(Integer.valueOf(array[0]));
			vo.setStudyType(Integer.valueOf(array[1]));
			List<Integer> clazzList = studentService.getFullClazz(
					Integer.valueOf(array[0]), Integer.valueOf(array[1]));
			headers.add("课程分类");
			headers.add("课程编号");
			headers.add("课程名称");
			headers.add("上课时间");
			for(Integer v : clazzList) {
				headers.add(TermView.getGradeName(vo.getGrade(),vo.getStudyType())+v+"班");
			}
			headers.add("总报名人数");
			headers.add("剩余名额");
			String[] header = String.join(",", headers).split(",");
			vo.setStudyType(Integer.valueOf(array[1]));
			List<Map<String, String>> resultList = new ArrayList<>();
			List<KccsCourseStudentSignTotal> totalList = kccsCourseInfoService.selectCourseClazzTotal(vo);
			Map<String, Integer> totalMap = new HashMap<String, Integer>();
			Map<String, Integer> gradeMap = new HashMap<String, Integer>();
			for(KccsCourseStudentSignTotal total : totalList) {
				totalMap.put(total.getCourseId()+total.getSignClassDate()+total.getGrade()+total.getClazz(), total.getNum());
				String gradeKey = total.getCourseId()+total.getSignClassDate();
				if(!gradeMap.containsKey(total.getCourseId()+total.getSignClassDate())) {
					gradeMap.put(gradeKey, total.getNum());
					continue;
				}
				gradeMap.put(gradeKey, gradeMap.get(gradeKey)+total.getNum());
			}
			vo.setIsAgree(1);
			Page<KccsCourseInfo> page = new Page<>();
			page.setPageSize(Integer.MAX_VALUE - 1);
			page.setParams(vo);
			List<OrderBy> orderBys = new ArrayList<>();
			orderBys.add(new OrderBy("COURSE_NO"));
			page.setSorts(orderBys);
			List<KccsCourseInfo> list = kccsCourseInfoService.selectListByPage(page);
			for(KccsCourseInfo kc : list) {
				if(StringUtils.isNotBlank(kc.getClassDate())) {
					String[] cs = kc.getClassDate().split(",");
					for(String c : cs) {
						Map<String, String> map = new LinkedHashMap<>();
						map.put("课程分类", kc.getCourseType());
						map.put("课程编号", kc.getCourseNo());
						map.put("课程名称", kc.getCourseName());
						map.put("上课时间", c);
						for(Integer clazz : clazzList) {
							Integer num = totalMap.get(kc.getId()+c+vo.getGrade()+clazz);
							map.put(TermView.getGradeName(vo.getGrade(),vo.getStudyType())+clazz+"班", String.valueOf(num != null ? num : 0));
						}
						Integer totalNum = gradeMap.get(kc.getId()+c);
						totalNum = totalNum == null ? 0 : totalNum;
						map.put("总报名人数", String.valueOf(totalNum));
						map.put("剩余名额", String.valueOf(kc.getLimitNum().intValue() - totalNum.intValue()));
						resultList.add(map);
					}
				}
			}
			//数据源
			LinkedHashMap<String, List> map = new LinkedHashMap<String, List>();
			String title="课程班级报名统计表";
			map.put(title, resultList);
			
			//标题  和  标题变量
		    List<String[]> headNames = new ArrayList<String[]>();
		    headNames.add(header);
		    List<String[]> fieldNames = new ArrayList<String[]>();
		    fieldNames.add(header);
		    
		    String [] titles=new String[] {title};
		    //excel生成帮助类
		    ExportSetInfoUtil setInfo = new ExportSetInfoUtil(map,titles,headNames,fieldNames,null);
		    request.setAttribute("exportDatas", setInfo);
		    
		    //生成
			modelAndView=new ModelAndView(new ExcelExportUtil(title+".xls",""));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return modelAndView;
	}
	
}
